/**
 * @Title: HomeActivity.java
 * @Package com.sinoglobal.waitingMe.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-8-30 上午11:55:09
 */

package com.sinoglobal.wallet.activity;

import java.util.ArrayList;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.adapter.W_BalanceDetailAdapter;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.entity.W_BalanceEntity;
import com.sinoglobal.wallet.entity.W_BalanceEntity.RsEntity;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.LogUtils;
import com.sinoglobal.wallet.util.NetWorkUtil;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnFooterRefreshListener;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnHeaderRefreshListener;

/**
 * @author clf
 * @createdate 2015-10-22
 * @Description: TODO(余额明细)
 */
public class W_BalanceDetailActivity extends W_SinoBaseActivity implements OnFooterRefreshListener, OnHeaderRefreshListener {

	private TextView spending, income;
	private PopupWindow menuPop;
	private ArrayList<RsEntity> list = new ArrayList<RsEntity>();
	private ArrayList<RsEntity> incomeList = new ArrayList<RsEntity>();
	private ArrayList<RsEntity> spendingList = new ArrayList<RsEntity>();
	private WPullToRefreshView baldetail_pulltorefresh;
	private ListView baldetail_listview;
	private W_BalanceDetailAdapter adapter;
	private LinearLayout baldetail_help_layout, baldetail_help_layouttwo;
	private TextView baldetail_help, baldetail_prompt, baldetail_prompttwo;
	private int page = 1;
	private String count = "10";
	private String amountType = "";
	private boolean isShowProgressDialog = true;

	private int totalpages;// 总页数
	private TextView notice_text;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_balace_detail_layout);
		mTemplateTitleText.setText("余额明细");
		showView(mRightLayout);
		mTemplateRightImg.setImageResource(R.drawable.w_content_spanner_more);
		initView();
		getData();
		showPopupWindow();
	}

	@Override
	public void onHeaderRefresh(WPullToRefreshView view) {
		baldetail_pulltorefresh.setEnablePullLoadMoreDataStatus(true);
		isShowProgressDialog = false;
		page = 1;
		getData();
	}

	@Override
	public void onFooterRefresh(WPullToRefreshView view) {
		isShowProgressDialog = false;
		if (page <= totalpages) {
			getData();

		} else {
			Toast.makeText(W_BalanceDetailActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
			baldetail_pulltorefresh.setEnablePullLoadMoreDataStatus(false);
			baldetail_pulltorefresh.onFooterRefreshComplete();
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		adapter = new W_BalanceDetailAdapter(this);
		baldetail_pulltorefresh = (WPullToRefreshView) findViewById(R.id.baldetail_pulltorefresh);
		baldetail_listview = (ListView) findViewById(R.id.baldetail_listview);
		baldetail_help_layout = (LinearLayout) findViewById(R.id.baldetail_help_layout);
		baldetail_help_layouttwo = (LinearLayout) findViewById(R.id.baldetail_help_layouttwo);
		baldetail_help = (TextView) findViewById(R.id.baldetail_help);
		baldetail_prompttwo = (TextView) findViewById(R.id.baldetail_prompttwo);
		baldetail_prompt = (TextView) findViewById(R.id.baldetail_prompt);
		baldetail_help.setText(Html.fromHtml("<u>" + "帮助" + "</u>"));
		notice_text = (TextView) findViewById(R.id.notice_text);
		baldetail_help.setOnClickListener(this);
		notice_text.setOnClickListener(this);
		baldetail_pulltorefresh.setOnFooterRefreshListener(this);
		baldetail_pulltorefresh.setOnHeaderRefreshListener(this);
		baldetail_listview.setAdapter(adapter);
	}

	private void showPopupWindow() {
		LinearLayout view = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.wallet_shop_menu_goodsdetail, null);

		spending = (TextView) view.findViewById(R.id.spending);
		spending.setOnClickListener(this);

		income = (TextView) view.findViewById(R.id.income);
		income.setOnClickListener(this);

		menuPop = new PopupWindow(view, this.getResources().getDimensionPixelSize(R.dimen.px158), LayoutParams.WRAP_CONTENT, true);
		ColorDrawable drawable = new ColorDrawable(0x00000000);
		menuPop.setBackgroundDrawable(drawable);
		// menuPop.showAsDropDown(v, 10, 0);
		/* 设置触摸外面时消失 */
		menuPop.setOutsideTouchable(true);
		/* 设置系统动画 */
		menuPop.setAnimationStyle(android.R.style.Animation_Dialog);
		menuPop.update();
		menuPop.setTouchable(true);
		/* 设置点击menu以外其他地方以及返回键退出 */
		menuPop.setFocusable(true);
		LogUtils.v("menuPop.高......" + menuPop.getHeight() + "menuPop.宽..." + menuPop.getWidth());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public void onClick(View v) {
		try {
			if (menuPop.isShowing()) {
				menuPop.dismiss();
			}
		} catch (Exception e) {
		}
		int id = v.getId();
		if (id == R.id.wallet_right_layout) {
			/*
			 * LogUtils.v("11111111111左........"+mTemplateRightImg.getLeft()+
			 * "111111111111111右...."+mTemplateRightImg.getRight());
			 * LogUtils.v("1111111111上........"
			 * +mTemplateRightImg.getTop()+"11111111111111.下..."
			 * +mTemplateRightImg.getBottom());
			 * LogUtils.v("222222222222222左........"
			 * +mRightLayout.getLeft()+"222222222右...."
			 * +mRightLayout.getRight());
			 * LogUtils.v("222222222上........"+mRightLayout
			 * .getTop()+"22222222222222下...."+mRightLayout.getBottom());
			 * LogUtils.v("33333333333左........"+tv_statusbar_room.getLeft()+
			 * "33333333333右...."+tv_statusbar_room.getRight());
			 * LogUtils.v("333333333333上........"
			 * +tv_statusbar_room.getTop()+"33333333333下...."
			 * +tv_statusbar_room.getBottom());
			 * LogUtils.v("444444444左........"+index_layout
			 * .getLeft()+"44444444右...."+index_layout.getRight());
			 * LogUtils.v("4444444444上........"
			 * +index_layout.getTop()+"44444444444下...."
			 * +index_layout.getBottom());
			 */// 记录一下校准popuwindon准确位置的艰苦。删不得。好烦
			menuPop.showAtLocation(mTemplateRightImg, Gravity.NO_GRAVITY, mTemplateRightImg.getRight() - mTemplateRightImg.getLeft() + mRightLayout.getLeft() + mTemplateRightImg.getLeft() - this.getResources().getDimensionPixelSize(R.dimen.px158), (mTemplateRightImg.getBottom() - mTemplateRightImg.getTop()) / 2 + tv_statusbar_room.getBottom() + mTemplateRightImg.getTop());
			// menuPop.showAsDropDown(v,
			// -this.getResources().getDimensionPixelSize(R.dimen.px158)/2+mTemplateRightImg.getWidth()/2,0);
		} else if (id == R.id.income) {// 收入
			// getIncomeList();
			page = 1;
			amountType = "0";
			list.clear();
			getData();
		} else if (id == R.id.spending) {// 支出
			page = 1;
			amountType = "1";
			list.clear();
			getData();
			// getSpendingList();
		} else if (id == R.id.baldetail_help) {// 帮助
			goIntent(W_BalanceDetailHelpActivity.class);
		} else if (id == R.id.notice_text) {// 捞饺子
			goIntent("com.sinoglobal.dumping.activity.MainActivity.HGQ");
		} else if (id == R.id.wallet_noData) {
			if (NetWorkUtil.getNetWorkInfoType(getApplicationContext()) == NetWorkUtil.NONE) {
				showToast("请检查网络是否连接！");
				loadError(true);
			} else {
				loadError(false);
				getData();
			}
		}
	}

	private void getIncomeList() {
		incomeList.clear();
		for (RsEntity list1 : list) {
			if (list1.getAmountType() == 0) {
				incomeList.add(list1);
			}
		}
		adapter.setListData(incomeList);

	}

	private void getSpendingList() {
		spendingList.clear();
		for (RsEntity list1 : list) {
			if (list1.getAmountType() == 1) {
				spendingList.add(list1);
			}
		}
		adapter.setListData(spendingList);

	}

	private void getData() {
		MyAsyncTask<W_BalanceEntity> task = new MyAsyncTask<W_BalanceEntity>(true, this, true) {
			@Override
			public W_BalanceEntity before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getDetail(W_BalanceDetailActivity.this, page + "", amountType, count);
			}

			@Override
			public void after(W_BalanceEntity result) {
				if (result != null) {
					if (result.getCode().equals("100")) {

						loadError(false);
						if (result.getRs().size() == 0) {
							if (amountType.equals("")) {
								baldetail_help_layout.setVisibility(View.VISIBLE);
								baldetail_help_layouttwo.setVisibility(View.GONE);
							} else if (amountType.equals("0")) {
								baldetail_help_layouttwo.setVisibility(View.VISIBLE);
								baldetail_help_layout.setVisibility(View.GONE);
								baldetail_prompttwo.setText("还没有收入明细哦~");
							} else if (amountType.equals("1")) {
								baldetail_help_layouttwo.setVisibility(View.VISIBLE);
								baldetail_help_layout.setVisibility(View.GONE);
								baldetail_prompttwo.setText("还没有支出明细哦~");
							}
							return;
						}
						baldetail_help_layout.setVisibility(View.GONE);
						baldetail_help_layouttwo.setVisibility(View.GONE);
						if (page == 1) {
							list = (ArrayList<RsEntity>) result.getRs();
							adapter.setListData(list);

						} else {
							list.addAll(result.getRs());
						}

						adapter.setListData(list);

						totalpages = result.getNum();

						page++;
						baldetail_pulltorefresh.onFooterRefreshComplete();
						baldetail_pulltorefresh.onHeaderRefreshComplete();
					} else {
						baldetail_pulltorefresh.onFooterRefreshComplete();
						baldetail_pulltorefresh.onHeaderRefreshComplete();
						Toast.makeText(W_BalanceDetailActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					baldetail_pulltorefresh.onFooterRefreshComplete();
					baldetail_pulltorefresh.onHeaderRefreshComplete();
					Toast.makeText(W_BalanceDetailActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
}
